package com.fzy.modules.people.web;

import com.alibaba.druid.util.StringUtils;
import com.fzy.core.common.ApiController;
import com.fzy.core.common.ResponseData;
import com.fzy.modules.people.entity.User;
import com.fzy.modules.people.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户Controller
 */
@RestController
@RequestMapping(value = "/api/people/user")
public class UserController extends ApiController {
    @Autowired
    UserService userService;
    @GetMapping(value = "")
    public List<User> getUseful(){
        return userService.getUseful(null);
    }
    /**
     * 移动端登录
     * @param user
     * @return
     */
    @PostMapping(value="/mobileLogin")
    public ResponseEntity<ResponseData> mobileLogin(User user){
    	List<User> users = userService.getUseful(user.getLoginName());
    	if(StringUtils.isEmpty(user.getLoginName())||StringUtils.isEmpty(user.getPassword())){
    		return new ResponseEntity<>(fail("用户名或密码不合法！"), HttpStatus.BAD_GATEWAY);
    	}
    	
    	if(users.isEmpty()||users.size()!=1){
    		return new ResponseEntity<>(fail("用户名不存在或用户名不可用！"), HttpStatus.BAD_GATEWAY);
        }
    	user=userService.login(user);
    	if(user==null){
    		return new ResponseEntity<>(fail("用户名或密码错误！"), HttpStatus.BAD_GATEWAY);
    	}
    	user.setPassword(null);//为了安全，清除密码
    	return new ResponseEntity<>(success(user), HttpStatus.OK);
    }
    @GetMapping(value = "/validateUserStatus")
    public ResponseEntity<ResponseData> validateUserStatus(User user){
    	int status=userService.findStatusById(user);
    	if(status!=1){
    		return new ResponseEntity<>(fail(), HttpStatus.BAD_GATEWAY);
    	}
    	return new ResponseEntity<>(success(), HttpStatus.OK);
    }
}
